package com.huijie.core.util.http;

import com.huijie.core.constant.Constants;
import com.huijie.core.util.AbstractCommonUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Alex
 * @ClassName: ClientIpUtil
 * @Description: 获取客户端IP地址
 * @date 2018年6月29日 下午5:40:02
 */
public class ClientIpUtil {

    private static final String MY_IP_SERVER = "本地服务器";
    private static final String UNKNOWN = "unknow";
    private static final String LOCAL_IP = "127.0.0.1";

    /**
     * 获取客户端IP地址
     *
     * @param request 请求
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (LOCAL_IP.equals(ip)) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                if (null != inet) {
                    ip = inet.getHostAddress();
                }
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        int ipLength = 15;
        if (ip != null && ip.length() > ipLength) {
            if (ip.indexOf(Constants.COMMA_SYMBOL) > 0) {
                ip = ip.substring(0, ip.indexOf(Constants.COMMA_SYMBOL));
            }
        }
        return ip;
    }

    /**
     * 获取IP返回地理位置
     *
     * @param ipAddr ip地址
     * @return
     */
    public static String getIpAddrSource(String ipAddr) {
        if (null != ipAddr) {
            String url = Constants.IP_INFO_API_URL;
            Map<String, Object> param = new HashMap<>(10);
            param.put("ip", ipAddr);
            String result = HttpUtil.post(url, param);
            if (StringUtils.isNotEmpty(result) && AbstractCommonUtil.isJSONValid(result)) {
                JSONObject obj = JSONObject.fromObject(result);
                //查询返回结果成功
                String returnCodeKey = "code";
                String successCode = "0";
                if (successCode.equals(obj.get(returnCodeKey).toString())) {
                    JSONObject obj2 = (JSONObject) obj.get("data");
                    return obj2.get("region") + " " + obj2.get("city");
                }
            } else {
                return MY_IP_SERVER;
            }
        }
        return null;
    }
}